<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>低代码页面拖拽编辑器开发 --- 2. 组件拖拽的优化 | 云游</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="favicon.ico">
    <script src="/live2dw/L2Dwidget.min.js"></script>
    <script src="/live2dw/L2Dwidget.0.min.js"></script>
    <script src="/live2dw/initLine2dw.js"></script>
    <link rel="stylesheet" href="/live2dw/initLine2dw.css">
    <meta name="description" content="仰天大笑出门去">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.746f5c46.css" as="style"><link rel="preload" href="/assets/js/app.8ca67ad1.js" as="script"><link rel="preload" href="/assets/js/3.dc5e7bc7.js" as="script"><link rel="preload" href="/assets/js/1.f19b5812.js" as="script"><link rel="preload" href="/assets/js/67.ceeafab4.js" as="script"><link rel="prefetch" href="/assets/js/10.063f4d84.js"><link rel="prefetch" href="/assets/js/11.7aaafe6d.js"><link rel="prefetch" href="/assets/js/12.1d3cbcb7.js"><link rel="prefetch" href="/assets/js/13.3eff3441.js"><link rel="prefetch" href="/assets/js/14.d011e66e.js"><link rel="prefetch" href="/assets/js/15.64da65a0.js"><link rel="prefetch" href="/assets/js/16.afe5f3d7.js"><link rel="prefetch" href="/assets/js/17.9645712f.js"><link rel="prefetch" href="/assets/js/18.8acac1b3.js"><link rel="prefetch" href="/assets/js/19.211aa7fa.js"><link rel="prefetch" href="/assets/js/20.33c40850.js"><link rel="prefetch" href="/assets/js/21.a5f61bc3.js"><link rel="prefetch" href="/assets/js/22.3f31af86.js"><link rel="prefetch" href="/assets/js/23.c1a2f360.js"><link rel="prefetch" href="/assets/js/24.f92207d5.js"><link rel="prefetch" href="/assets/js/25.3859fc8e.js"><link rel="prefetch" href="/assets/js/26.c3bb43ea.js"><link rel="prefetch" href="/assets/js/27.2d3ac28b.js"><link rel="prefetch" href="/assets/js/28.5eba4e00.js"><link rel="prefetch" href="/assets/js/29.591a72c1.js"><link rel="prefetch" href="/assets/js/30.4b4e3dae.js"><link rel="prefetch" href="/assets/js/31.0c3aac1f.js"><link rel="prefetch" href="/assets/js/32.927b230d.js"><link rel="prefetch" href="/assets/js/33.4ecffa3d.js"><link rel="prefetch" href="/assets/js/34.b9bc1b48.js"><link rel="prefetch" href="/assets/js/35.42f94296.js"><link rel="prefetch" href="/assets/js/36.95b6cc33.js"><link rel="prefetch" href="/assets/js/37.f3505924.js"><link rel="prefetch" href="/assets/js/38.19c4c627.js"><link rel="prefetch" href="/assets/js/39.592f8fae.js"><link rel="prefetch" href="/assets/js/4.47f43f55.js"><link rel="prefetch" href="/assets/js/40.e4393725.js"><link rel="prefetch" href="/assets/js/41.66b5758c.js"><link rel="prefetch" href="/assets/js/42.b0eb455d.js"><link rel="prefetch" href="/assets/js/43.11fab467.js"><link rel="prefetch" href="/assets/js/44.cd6e5a73.js"><link rel="prefetch" href="/assets/js/45.b3e3b872.js"><link rel="prefetch" href="/assets/js/46.07a07f51.js"><link rel="prefetch" href="/assets/js/47.c2287444.js"><link rel="prefetch" href="/assets/js/48.66123573.js"><link rel="prefetch" href="/assets/js/49.bebbe116.js"><link rel="prefetch" href="/assets/js/5.e8c05b51.js"><link rel="prefetch" href="/assets/js/50.70436f90.js"><link rel="prefetch" href="/assets/js/51.18985c0f.js"><link rel="prefetch" href="/assets/js/52.ace084da.js"><link rel="prefetch" href="/assets/js/53.02616c91.js"><link rel="prefetch" href="/assets/js/54.6eb33990.js"><link rel="prefetch" href="/assets/js/55.733d5e7a.js"><link rel="prefetch" href="/assets/js/56.36ca4523.js"><link rel="prefetch" href="/assets/js/57.80493e39.js"><link rel="prefetch" href="/assets/js/58.34b95d6c.js"><link rel="prefetch" href="/assets/js/59.96be3833.js"><link rel="prefetch" href="/assets/js/6.027fb3c9.js"><link rel="prefetch" href="/assets/js/60.214c4180.js"><link rel="prefetch" href="/assets/js/61.ba0ebff0.js"><link rel="prefetch" href="/assets/js/62.09089206.js"><link rel="prefetch" href="/assets/js/63.a5c5aa3e.js"><link rel="prefetch" href="/assets/js/64.2d5fc9ca.js"><link rel="prefetch" href="/assets/js/65.c8171d3c.js"><link rel="prefetch" href="/assets/js/66.6d4bbf9e.js"><link rel="prefetch" href="/assets/js/68.16d3218f.js"><link rel="prefetch" href="/assets/js/7.b5f855a8.js"><link rel="prefetch" href="/assets/js/8.5ac3177c.js"><link rel="prefetch" href="/assets/js/9.8e7f0e06.js">
    <link rel="stylesheet" href="/assets/css/0.styles.746f5c46.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>云游</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>仰天大笑出门去</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>fhtwl</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">云游</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/其它/" class="nav-link"><i class="undefined"></i>
  其它
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/fhtwl/fhtwl.github.io" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><!----> <h3 class="name" data-v-39576ba9>
    fhtwl
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>57</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>64</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/其它/" class="nav-link"><i class="undefined"></i>
  其它
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/fhtwl/fhtwl.github.io" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>低代码页面拖拽编辑器开发 --- 2. 组件拖拽的优化</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>fhtwl</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-1aefc0b4><main class="page" style="padding-right:0;"><section><div class="page-title"><h1 class="title">低代码页面拖拽编辑器开发 --- 2. 组件拖拽的优化</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>fhtwl</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>1/27/2022</span></i> <i class="iconfont reco-eye" data-v-f875f3fc><span id="/blog/vue/31.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-f875f3fc><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>低代码平台</span><span class="tag-item" data-v-f875f3fc>vue.js</span><span class="tag-item" data-v-f875f3fc>js</span><span class="tag-item" data-v-f875f3fc>lowCode</span></i></div></div> <div class="theme-reco-content content__default"><p>接上篇 <a href="/blog/vue/30.html">低代码页面拖拽编辑器开发 --- 1. 项目主要功能和实现思路</a> <br></p> <p>这一片主要是对组件拖动相关功能的优化和深入</p> <h2 id="_2-程序功能"><a href="#_2-程序功能" class="header-anchor">#</a> 2. 程序功能</h2> <p>...<br>
5. 组件的缩放</p> <p>当点击组件时, 在组件边框的角和中点显示缩放点, 拖动这些点时, 组件会随着鼠标方法缩小</p> <div style="text-align:center;"><img src="/images/20220127/缩放.jpg"></div> <ul><li>缩放点的绘制</li></ul> <p>绘制8个点</p> <div class="language-html extra-class"><pre class="language-html"><code>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span>
    <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>zoom-point<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">v-for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>(item, index) in directionList<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">:key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>index<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">:data-index</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>index<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">data-type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>zoom-point<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">draggable</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>false<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">:style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>getPointStyle(index)<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>

</code></pre></div><p>从左上角开始, 根据index来得到点的坐标与手型</p> <div class="language-js extra-class"><pre class="language-js"><code>  <span class="token function">getPointStyle</span><span class="token punctuation">(</span><span class="token parameter">index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> newLeft <span class="token operator">=</span> <span class="token number">0</span>
    <span class="token keyword">let</span> newTop <span class="token operator">=</span> <span class="token number">0</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span>
        newLeft <span class="token operator">=</span> <span class="token number">0</span>
        newTop <span class="token operator">=</span> <span class="token number">0</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>
        newLeft <span class="token operator">=</span> <span class="token string">&quot;50%&quot;</span>
        newTop <span class="token operator">=</span> <span class="token number">0</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span>
        newLeft <span class="token operator">=</span> <span class="token string">&quot;100%&quot;</span>
        newTop <span class="token operator">=</span> <span class="token number">0</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token number">3</span><span class="token operator">:</span>
        newLeft <span class="token operator">=</span> <span class="token string">&quot;100%&quot;</span>
        newTop <span class="token operator">=</span> <span class="token string">&quot;50%&quot;</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token number">4</span><span class="token operator">:</span>
        newLeft <span class="token operator">=</span> <span class="token string">&quot;100%&quot;</span>
        newTop <span class="token operator">=</span> <span class="token string">&quot;100%&quot;</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token number">5</span><span class="token operator">:</span>
        newLeft <span class="token operator">=</span> <span class="token string">&quot;50%&quot;</span>
        newTop <span class="token operator">=</span> <span class="token string">&quot;100%&quot;</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token number">6</span><span class="token operator">:</span>
        newLeft <span class="token operator">=</span> <span class="token string">&quot;0%&quot;</span>
        newTop <span class="token operator">=</span> <span class="token string">&quot;100%&quot;</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token number">7</span><span class="token operator">:</span>
        newLeft <span class="token operator">=</span> <span class="token string">&quot;0%&quot;</span>
        newTop <span class="token operator">=</span> <span class="token string">&quot;50%&quot;</span>
        <span class="token keyword">break</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">const</span> style <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">marginLeft</span><span class="token operator">:</span> <span class="token string">&quot;-4px&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">marginTop</span><span class="token operator">:</span> <span class="token string">&quot;-4px&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">left</span><span class="token operator">:</span> newLeft<span class="token punctuation">,</span>
        <span class="token literal-property property">top</span><span class="token operator">:</span> newTop<span class="token punctuation">,</span>
        <span class="token literal-property property">cursor</span><span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token keyword">this</span><span class="token punctuation">.</span>angleToCursor<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">.</span>cursor<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">-resize</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> style
  <span class="token punctuation">}</span><span class="token punctuation">,</span>

</code></pre></div><ul><li>缩放点的拖动</li></ul> <p>交互的思路与组件拖动的思路相同, 监听鼠标的mousedown, mousemove和mouseup, 并通过鼠标的绝对位置计算鼠标的移动量 <br>
除去鼠标偏移量外, 还需要一个参数, 那就是当前缩放点的index. 根据index, 来判断当前拖动的缩放点 <br>
若index为0即拖动的是左上角的点时, 则组件的width、height、left、top都会随之变化, 以此类推</p> <div class="language-js extra-class"><pre class="language-js"><code>  <span class="token comment">// 点击组件</span>
  <span class="token function">handleComponentMouseDown</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    event<span class="token punctuation">.</span><span class="token function">preventDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    event<span class="token punctuation">.</span><span class="token function">stopPropagation</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span>type<span class="token punctuation">,</span> index<span class="token punctuation">}</span> <span class="token operator">=</span> event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>dataset
    <span class="token comment">// 如果点击的是缩放点</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>type <span class="token operator">===</span> <span class="token string">&quot;zoom-point&quot;</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token comment">// 开始拖拽时的初始位置</span>
      mouseZoomStart <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">x</span><span class="token operator">:</span> event<span class="token punctuation">.</span>clientX<span class="token punctuation">,</span>
        <span class="token literal-property property">y</span><span class="token operator">:</span> event<span class="token punctuation">.</span>clientY
      <span class="token punctuation">}</span>

      <span class="token comment">// 表示可以缩放</span>
      isMouseZoomDown <span class="token operator">=</span> <span class="token boolean">true</span>
      <span class="token comment">// 记录缩放点的index</span>
      selectZoomIndex <span class="token operator">=</span> index
    <span class="token punctuation">}</span>
    

  <span class="token punctuation">}</span>

  <span class="token function">handleMouseMove</span> <span class="token punctuation">(</span><span class="token parameter">event<span class="token punctuation">,</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    event<span class="token punctuation">.</span><span class="token function">preventDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    event<span class="token punctuation">.</span><span class="token function">stopPropagation</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>isMouseZoomDown<span class="token punctuation">)</span><span class="token punctuation">{</span> 
      <span class="token keyword">return</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 开始位置</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span>x<span class="token punctuation">,</span>y<span class="token punctuation">}</span> <span class="token operator">=</span> mouseZoomStart
    <span class="token comment">// 获取鼠标的偏移量</span>
    <span class="token keyword">const</span> offsetX <span class="token operator">=</span> event<span class="token punctuation">.</span>clientX <span class="token operator">-</span> x
    <span class="token keyword">const</span> offsetY <span class="token operator">=</span> event<span class="token punctuation">.</span>clientY <span class="token operator">-</span> y
    <span class="token keyword">let</span> newWidth<span class="token punctuation">;</span> <span class="token keyword">let</span> newHeight<span class="token punctuation">;</span> <span class="token keyword">let</span> newLeft<span class="token punctuation">;</span> <span class="token keyword">let</span> newTop

    <span class="token keyword">const</span> <span class="token punctuation">{</span>
      width<span class="token punctuation">,</span>height<span class="token punctuation">,</span>top<span class="token punctuation">,</span>left
    <span class="token punctuation">}</span> <span class="token operator">=</span> selectedComponent<span class="token punctuation">.</span>style
    
    
    <span class="token comment">// 获取新几何属性</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span>selectZoomIndex<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">case</span> <span class="token string">&quot;0&quot;</span><span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span> left<span class="token punctuation">,</span> top
        newWidth <span class="token operator">=</span> width <span class="token operator">-</span> offsetX
        newHeight <span class="token operator">=</span> height <span class="token operator">-</span> offsetY
        newLeft <span class="token operator">=</span> left <span class="token operator">+</span> offsetX
        newTop <span class="token operator">=</span> top <span class="token operator">+</span> offsetY
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&quot;1&quot;</span><span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span> top
        newWidth <span class="token operator">=</span> width
        newHeight <span class="token operator">=</span> height <span class="token operator">-</span> offsetY
        newLeft <span class="token operator">=</span> left
        newTop <span class="token operator">=</span> top <span class="token operator">+</span> offsetY
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&quot;2&quot;</span><span class="token operator">:</span>
        newWidth <span class="token operator">=</span> width <span class="token operator">+</span> offsetX
        newHeight <span class="token operator">=</span> height <span class="token operator">-</span> offsetY
        newLeft <span class="token operator">=</span> left
        newTop <span class="token operator">=</span> top <span class="token operator">+</span> offsetY
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&quot;3&quot;</span><span class="token operator">:</span>
        newWidth <span class="token operator">=</span> width <span class="token operator">+</span> offsetX
        newHeight <span class="token operator">=</span> height
        newLeft <span class="token operator">=</span> left
        newTop <span class="token operator">=</span> top
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&quot;4&quot;</span><span class="token operator">:</span>
        newWidth <span class="token operator">=</span> width <span class="token operator">+</span> offsetX
        newHeight <span class="token operator">=</span> height <span class="token operator">+</span> offsetY
        newLeft <span class="token operator">=</span> left
        newTop <span class="token operator">=</span> top
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&quot;5&quot;</span><span class="token operator">:</span>
        newWidth <span class="token operator">=</span> width
        newHeight <span class="token operator">=</span> height <span class="token operator">+</span> offsetY
        newLeft <span class="token operator">=</span> left
        newTop <span class="token operator">=</span> top
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&quot;6&quot;</span><span class="token operator">:</span>
        newWidth <span class="token operator">=</span> width <span class="token operator">-</span> offsetX
        newHeight <span class="token operator">=</span> height <span class="token operator">+</span> offsetY
        newLeft <span class="token operator">=</span> left <span class="token operator">+</span> offsetX
        newTop <span class="token operator">=</span> top
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&quot;7&quot;</span><span class="token operator">:</span>
        newWidth <span class="token operator">=</span> width <span class="token operator">-</span> offsetX
        newHeight <span class="token operator">=</span> height
        newLeft <span class="token operator">=</span> left <span class="token operator">+</span> offsetX
        newTop <span class="token operator">=</span> top
        <span class="token keyword">break</span>
      <span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token keyword">break</span>
    <span class="token punctuation">}</span>
    component<span class="token punctuation">.</span>style<span class="token punctuation">.</span>left <span class="token operator">=</span> newLeft
    component<span class="token punctuation">.</span>style<span class="token punctuation">.</span>top <span class="token operator">=</span> newTop
    component<span class="token punctuation">.</span>style<span class="token punctuation">.</span>width <span class="token operator">=</span> newWidth
    component<span class="token punctuation">.</span>style<span class="token punctuation">.</span>left <span class="token operator">=</span> newHeight

    <span class="token comment">// 然后将现在的位置赋值给初始位置</span>
    selectedMouseOffset <span class="token operator">=</span> <span class="token punctuation">{</span>
      <span class="token literal-property property">x</span><span class="token operator">:</span> event<span class="token punctuation">.</span>clientX<span class="token punctuation">,</span>
      <span class="token literal-property property">y</span><span class="token operator">:</span> event<span class="token punctuation">.</span>clientY
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  handleMoudeUp <span class="token punctuation">{</span>
    isMouseZoomDown <span class="token operator">=</span> <span class="token boolean">false</span>
  <span class="token punctuation">}</span>

</code></pre></div><ol start="6"><li>组件嵌套</li></ol> <p>在实际是使用场景中, tabs(选项卡)组件是很有用的, 如果组件之间没有嵌套, 即所有组件都是同级, 那么对用户来说tabs组件是很难使用的, 因此组件的嵌套是必须的. <br></p> <p>组件嵌套最项目的主要影响, 在于整个画布里的组件列表, 变成了组件树. <br></p> <div class="language-jsx extra-class"><pre class="language-jsx"><code>
  <span class="token comment">// 渲染组件</span>
  <span class="token function">renderComponent</span><span class="token punctuation">(</span><span class="token parameter">component<span class="token punctuation">,</span> callback</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span>
      props
    <span class="token punctuation">}</span> <span class="token operator">=</span> component
    <span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token keyword">case</span> <span class="token string">&quot;c-tabs&quot;</span><span class="token operator">:</span>
        <span class="token keyword">return</span> <span class="token punctuation">(</span>
          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>c-tabs</span> <span class="token punctuation">&gt;</span></span><span class="token plain-text">
            </span><span class="token punctuation">{</span>
              <span class="token punctuation">(</span>props<span class="token punctuation">.</span>list <span class="token operator">||</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">tab</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token punctuation">(</span>
                  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a-tab-pane</span> <span class="token attr-name">key</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>tab<span class="token punctuation">.</span>label<span class="token punctuation">}</span></span> <span class="token attr-name">tab</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>tab<span class="token punctuation">.</span>label<span class="token punctuation">}</span></span><span class="token punctuation">&gt;</span></span><span class="token plain-text">
                    </span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">renderComponentList</span><span class="token punctuation">(</span>tab<span class="token punctuation">.</span>children<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token plain-text">
                  </span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a-tab-pane</span><span class="token punctuation">&gt;</span></span>
                  
                <span class="token punctuation">)</span>
              <span class="token punctuation">}</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token plain-text">
          </span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>c-tabs</span><span class="token punctuation">&gt;</span></span>
        <span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token comment">// 渲染组件列表</span>
  <span class="token function">renderComponentList</span><span class="token punctuation">(</span><span class="token parameter">list</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>
      list<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>renderComponent<span class="token punctuation">)</span>
    <span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token function">render</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">return</span> <span class="token punctuation">(</span>
      <span class="token function">renderComponentList</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>componentList<span class="token punctuation">)</span>
    <span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

</code></pre></div><ol start="7"><li>组件的框选与多选</li></ol> <p>框选的作用就是为了多选组件.</p> <ul><li>框选
框选的思路与之前的拖拽组件类似.绘制一个框选div, 设置绝对定位并隐藏, 鼠标点击画布时, 记录鼠标的绝对坐标(相对于屏幕可视区的坐标,clientX,clientY)和相对坐标(相对于画布即父元素的的坐标, offsetX, offsetY), 鼠标按住并移动时, 记录当前的位置。通过第一个点的相对坐标为point1, 然后通过两点的差值, 计算出point2, 并使用这两个对角点(point1, point2)绘制矩形.</li></ul> <ol start="8"><li>标线对齐与吸附</li></ol></div></section> <footer class="page-edit"><!----> <!----></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:0;" data-v-cb1513f6></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/assets/js/app.8ca67ad1.js" defer></script><script src="/assets/js/3.dc5e7bc7.js" defer></script><script src="/assets/js/1.f19b5812.js" defer></script><script src="/assets/js/67.ceeafab4.js" defer></script>
  </body>
</html>
